const mysql = require("mysql")
const { getType } = require("../auth/auth")

const db = mysql.createPool({
  host: "127.0.0.1",
  port: "3306",
  user: "root",
  password: "123456",
  database: "vue-admin",
})

const query = (sql, data, callback) => {
  const dataType = getType(data)
  // 只传入了两个参数，省略data
  if (dataType === "Function" && !callback) {
    callback = data
    data = null
  }
  db.getConnection((err, con) => {
    if (err) {
      callback(err)
    } else {
      con.query(sql, data, (err, res) => {
        con.release() // 释放连接
        callback(err, res)
      })
    }
  })
}

module.exports = query
